// user/meInfo/meInfo.js
import http from '../../http'
import {
  areaData
} from '../../config';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {
      avatar: '',
      nickName: '测试',
      gender: "0",
      fete: '2024-1-4',
      area: '郑州',
      phoneNumber: '1231213',
      email: 'xxx'
    },
    // 性别
    genderBol: false,
    genderList: [{
      label: '男',
      value: '1'
    }, {
      label: '女',
      value: '0'
    }, ],
    // 生日
    feteBol: false,
    end: new Date().getTime(),
    // 地址
    areaBol: false,
    areaData: areaData,
  },
  // 退出登录
  quit() {
    wx.removeStorageSync("userId");
    wx.removeStorageSync("wxCode");
    wx.switchTab({
      url: '/pages/user/user'
    })
  },
  head(event) {
    let detail = event.detail; // 获取用户选择的头像URL
    console.log(detail);
    http.file({
      url: '/Update/uploads',
      data: {},
      img: detail.avatarUrl,
      then: (res) => {
        console.log('上传文件', res);
        let data = JSON.parse(res.data)
        console.log(data);
        if (data.code == 1) {
          this.setData({
            'userInfo.avatar': data.data
          }, () => {
            this.userInfoChange('avatar')
          })
        } else {
          wx.showToast({
            title: data.msg,
            icon: 'error',
          })
        }
      },
      catch: (err) => {}
    })
  },
  onClickCell(e) {
    let data = e.currentTarget.dataset.type
    if (data == 'gender') {
      this.setData({
        genderBol: true
      })
    } else if (data == 'fete') {
      this.setData({
        feteBol: true
      })
    } else if (data == 'area') {
      this.setData({
        areaBol: true
      })
    } else if (data == 'autonym') {
      if (
        !this.data.userInfo.authentication
      ) {
        wx.navigateTo({
          url: `/user/meInfoAutonym/meInfoAutonym`
        })
      }

    } else {
      wx.navigateTo({
        url: `/user/meInfoEdit/meInfoEdit?type=${e.currentTarget.dataset.type}`
      })
    }
  },
  onInputValue(e) {
    let data = e.currentTarget.dataset.type
    console.log(e.detail);
    if (data == 'gender') {
      this.setData({
        'userInfo.gender': e.detail.value[0]
      })
    } else if (data == 'fete') {
      this.setData({
        'userInfo.birthday': e.detail.value,
        feteBol: false
      })
    } else if (data == 'area') {
      let area = e.detail.selectedOptions
      this.setData({
        'userInfo.region': area[0].label + '-' + area[1].label + '-' + area[2].label
      })
    }
    this.userInfoChange(data)
  },
  userInfoChange(type) {
    let data = {
      id: wx.getStorageSync('userId'),
    }
    if (type == 'gender') {
      data.gender = this.data.userInfo.gender
    } else if (type == 'fete') {
      data.birthday = this.data.userInfo.birthday
    } else if (type == 'area') {
      data.region = this.data.userInfo.region
    } else if (type == 'avatar') {
      data.avatar = this.data.userInfo.avatar
    }
    http.post({
      url: '/User/edit',
      data: data,
      then: (res) => {
        console.log('修改资料', res);
        if (res.data.code == 1) {
          wx.showToast({
            title: '修改成功',
            icon: 'success',
          })
        }
      }
    })
  },
  feteCancel() {
    this.setData({
      feteBol: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    http.post({
      url: '/User/info',
      data: {
        id: wx.getStorageSync('userId')
      },
      then: (res) => {
        console.log('用户信息', res);
        let data = res.data.data
        // data.avatar = "https://" + http.ip + data.avatar
        this.setData({
          userInfo: data,
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})